import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;

import ShipModel;
import haxe.FastList;
class BossShipController
{
  private static inline var EASING:Float = 0.1;
  public var subModels:FastList<CircleMV>;
  public var CenterModel:CircleModel;
  private var _playerModel:ShipModel;
  private var _stage:Dynamic;

  public function new(playerModel:ShipModel) 
  {
    subModels = new FastList<CircleMV>();
    _playerModel = playerModel;
    getCenter();
    _playerModel.addEventListener(Event.CHANGE, changeHandler);
  }

  public function getCenter()
  {
    var count:Int = 0;
    for(model in subModels)
      {
	count++;
      }
    if(count > 0)
    {
      var centerNum = Math.round(count / 2);
      count = 0;
      for(model in subModels)
      {
	count++;
	if(count == centerNum)
	{
	  CenterModel = model.model;
	  break;
	}
      }
    }

  }
  public function changeHandler(event:Event)
  {

    if(CenterModel == null)
    {getCenter();}

    if(CenterModel == null)
    {
      return;
    }
    var acceleration_X = -.1;
    var acceleration_Y = -.1;
       if(_playerModel.xPos < CenterModel.xPos && CenterModel.vx > -.7)
    {
      acceleration_X = -.1;
    }
    else if(_playerModel.xPos > CenterModel.xPos && CenterModel.vx < .7)
    {
      acceleration_X = .1;
    }
    else
    {
      acceleration_X = 0;
    }
    if(_playerModel.yPos < CenterModel.yPos && CenterModel.vy > -.7)
    {
      acceleration_Y = -.1;
    }
    else if(_playerModel.yPos > CenterModel.yPos && CenterModel.vy < .7)
    {
      acceleration_Y= .1;
    }
    else
    {
      acceleration_Y = 0;
    }
 

    for(model in subModels)
    {
      model.model.friction = 1;
      model.model.acceleration_X = acceleration_X;
      model.model.acceleration_Y = acceleration_Y;
    }
  }
}